﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class Fr_KhoGo : DevComponents.DotNetBar.Office2007Form
    {
        public Fr_KhoGo()
        {
            InitializeComponent();
        }
        private string CDay1, CRong1, CDai1, NoiGiao1, NoiNhan1, AdorKd1,SoXe1, LoaiGo1,SoCT1, SoBB1, Ngay1, SoLuong1, DVT1, PhanLoai1, KhachHang1, GhiChu1, MaCT1,string_temp,child=" - ",TenCT1,ChungTu1,LyDo1,Ngay_End1,Ngay_start1,NguonGo1,Number_temp,SoKhoi1;
        private double CDay_d, CRong_d, CDai_d, SoLuong_d,sum_temp,SoKhoi_d;
        private int i = 0, j = 0, check = 0;
        private void txt_CDayinput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CDayinput);
        }

        private void txt_CRonginput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CRonginput);
        }

        private void txt_CDaiinput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CDaiinput);
            if (txt_CDaiinput.Text != "")
            {
                CDai_d = double.Parse(txt_CDaiinput.Text);
                if (CDai_d >= 1000 && CDai_d % 100 == 0)
                {
                    cb_PLoaiinput.Text = "Phach";
                    
                }
                

               
            }
        }

        private void txt_SoLuonginput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuonginput);
        }

        private void txt_CDayoutput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CDayoutput);



            if (cb_AdorKDoutput.Text == "" || cb_LoaiGooutput.Text == "")
            {
                MessageBox.Show("Nhập đầy đủ thông tin: AD/KD, Loại Gỗ");
            }
            else
            {
                if (chk_MatLaoutput.Checked == true && chk_MatLaoutput.Visible == true)
                {
                    Process prs = new Process();
                    SqlConnection Conn = prs.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;

                    try
                    {
                        string_temp = @"select SLTon,SoKhoi from dbo.KhoGo_TK where LoaiGo='" + cb_LoaiGooutput.Text.Trim() + "' and ADorKD='" + cb_AdorKDoutput.Text.Trim() + "' and ChieuDay=" + txt_CDayoutput.Text + " and ChieuRong=0 and ChieuDai=0";
                        cmd.CommandText = string_temp;
                        SqlDataReader rd = cmd.ExecuteReader();
                        i = 0;
                        Number_temp = "0";
                        GhiChu1 = "0";
                        while (rd.Read())
                        {
                           
                            Number_temp = rd[0].ToString();
                            GhiChu1 = rd[1].ToString();
                        }
                        lb_TonKho.Text = Number_temp;
                        lb_KhoiTK.Text = GhiChu1;
                        

                        Conn.Close();
                        cmd.Dispose();
                        //temp.Clone();
                        rd.Close();

                    }
                    catch
                    {
                        lb_TonKho.Text = "0";
                        lb_KhoiTK.Text = "0";
                        MessageBox.Show("Xem lại thông tin nhập");
                    }
                    finally
                    {
                        Conn.Close();
                        cmd.Dispose();
                    }
                }
            }
        }

        private void txt_CRongoutput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CRongoutput);
        }

        private void txt_CDaioutput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_CDaioutput);
            
            if (cb_AdorKDoutput.Text == "" || cb_LoaiGooutput.Text == "" || txt_CRongoutput.Text == "" || txt_CDaioutput.Text == "")
            {
                MessageBox.Show("Nhập đầy đủ thông tin: AD/KD, Loại Gỗ, C/Dày,C/Rộng, C/Dài");
            }
            else
            {
                Process prs = new Process();
                SqlConnection Conn = prs.Connect();
                Conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;

                try
                {
                    if (chk_MatLaoutput.Checked == false || chk_MatLaoutput.Visible == false)
                        string_temp = @"select SLTon,SoKhoi from dbo.KhoGo_TK where LoaiGo='" + cb_LoaiGooutput.Text.Trim() + "' and ADorKD='" + cb_AdorKDoutput.Text.Trim() + "' and ChieuDay=" + txt_CDayoutput.Text + " and ChieuRong=" + txt_CRongoutput.Text + " and ChieuDai=" + double.Parse(txt_CDaioutput.Text).ToString();
                   
                    cmd.CommandText = string_temp;
                    SqlDataReader rd = cmd.ExecuteReader();
                    i = 0;
                    while (rd.Read())
                    {
                        //MessageBox.Show(string_temp);
                        Number_temp = rd[0].ToString();
                        GhiChu1 = rd[1].ToString();
                    }
                    lb_TonKho.Text = Number_temp;
                    lb_KhoiTK.Text = GhiChu1;
                    Conn.Close();
                    cmd.Dispose();
                    //temp.Clone();
                    rd.Close();
                }
                catch
                {
                    lb_TonKho.Text = "0";
                    MessageBox.Show("Xem lại thông tin nhập");
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }

        private void txt_SoLuongoutput_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuongoutput);
            if (txt_SoLuongoutput.Text != "")
            {
                if (chk_MatLaoutput.Visible == false || chk_MatLaoutput.Checked == false)
                {
                    if (double.Parse(txt_SoLuongoutput.Text) > double.Parse(lb_TonKho.Text))
                    {
                        MessageBox.Show("Số lượng xuất không vượt quá số lượng tồn kho");
                        txt_SoLuongoutput.Text = "";
                    }
                }
                else
                {
                    if (double.Parse(txt_CDaioutput.Text) > double.Parse(lb_KhoiTK.Text))
                    {
                        MessageBox.Show("Số lượng xuất không vượt quá số lượng tồn kho");
                        txt_SoLuongoutput.Text = "";
                    }
                }
               
            }
        }
        private void combo_load()
        {
            Data dta = new Data();
            FormProcess Frp = new FormProcess();
            string_temp = @"select TenNN,MaNN from dbo.NoiNhan";
            SqlConnection Conn = dta.Connect();
            
            Conn.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandText = string_temp;
            try
            {
                SqlDataReader rd = cmd.ExecuteReader();

                while (rd.Read())
                {
                    cb_NoiGiaoinput.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiaooutput.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhaninput.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhanoutput.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NguonGooutput.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiaoreport.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhanreport.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    //.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                }
                string[] arr1 = new string[] {"AD","KD","Khác"};
                for (i = 0; i < arr1.Length; i++)
                {
                    cb_AdorKDinput.Items.Add(arr1[i].ToString());
                    cb_AdorKDoutput.Items.Add(arr1[i].ToString());
                    cb_AdorKDreport.Items.Add(arr1[i].ToString());
                }
                string[] arr2 = new string[] { "Thanh", "Cái", "Kg", "Bịch", "VNĐ", "Con" };
                for (i = 0; i < arr2.Length; i++)
                {
                    cb_DVTinput.Items.Add(arr2[i].ToString());
                    cb_DVToutput.Items.Add(arr2[i].ToString());
                }
                string[] arr3 = new string[] { "Phach", "So Che", "Van" };
                for (i = 0; i < arr3.Length; i++)
                {
                    cb_PLoaiinput.Items.Add(arr3[i].ToString());
                    cb_PLoaioutput.Items.Add(arr3[i].ToString());
                }
                string[] arr4 = new string[] { "ac","eu" };
                for (i = 0; i < arr4.Length; i++)
                {
                    cb_LoaiGoinput.Items.Add(arr4[i].ToString());
                    cb_LoaiGooutput.Items.Add(arr4[i].ToString());
                    cb_LoaiGoreport.Items.Add(arr4[i].ToString());
                }
                string_temp=@"select MaChiTiet,TenChiTiet from dbo.Load_CTSP_Mua";
                Frp.cb1(cb_MaCTinput, string_temp, 1, 0);
                Frp.cb1(cb_MaCToutput, string_temp, 1, 0);
                string_temp = @"select MaLyDo,LyDo from dbo.LyDo where KV='KhoGo'";
                Frp.cb1(cb_LyDoinput, string_temp, 1, 0);
                Frp.cb1(cb_LyDooutput, string_temp, 1, 0);
                string_temp = @"SELECT     QuyCach, convert(char(4),CDay)+'x'+convert(char(5),CRong)+'x'+convert(char(6),CDai)
                FROM         dbo.QuyCach";
               

                Frp.cb1(cb_QuyCachinput, string_temp, 0, 1);

                Frp.cb1(cb_QuyCachoutput, string_temp, 0, 1);
                arr1.Clone();
                arr2.Clone();
                arr3.Clone();
                arr4.Clone();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void Enable()
        {
            tab_Nhap.Visible = true;
            tab_Xuat.Visible = true;
        }
        private void Disable()
        {
            tab_Nhap.Visible = false;
            tab_Xuat.Visible = false;
        }
        private void Check_User()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                string check = @"select GroupUser from dbo.Users where UsersName='" + lb_user.Text + "'";
                cmd.Connection = Conn;
                cmd.CommandText = check;
                //MessageBox.Show(check);
                check = cmd.ExecuteScalar().ToString();
                if (check == "thongke" || check == "admin" || check == "nhaplieu")
                    Enable();
                else
                    Disable();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        public void Users(string Users)
        {
            lb_user.Text = Users;
        }


        private void Fr_KhoGo_Load(object sender, EventArgs e)
        {
            combo_load();
        }
        private bool check_combobox(ComboBox cbx,string check)
        {
            i = cbx.Items.Count - 1;
            while (i >= 0)
            {
                if (check == cbx.Items[i].ToString())
                    break;
                else
                    i--;
            }
            if (i >= 0)
                return true;
            else
                return false;
        }
        private void add_grid()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                if (cb_NoiGiaoinput.Text == "" || cb_NoiNhaninput.Text == "" || cb_LoaiGoinput.Text == "" || txt_CDayinput.Text == "" || txt_CRonginput.Text == "" || txt_CDaiinput.Text == "" || txt_SoLuonginput.Text == ""||cb_PLoaiinput.Text==""||txt_SoBBinput.Text==""||cb_LyDoinput.Text=="")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaoinput.Text, child).Trim();
                    NoiNhan1 = prs.substring_right(cb_NoiNhaninput.Text, child).Trim() ;
                    LyDo1 = prs.substring_right(cb_LyDoinput.Text, child).Trim();
                    if (NoiGiao1 == "" || NoiNhan1 == ""||LyDo1=="")
                        MessageBox.Show("Nhập sai thông tin");
                    else
                    {
                        
                        string[] temp = txt_Ngayinput.Text.Split('/');
                        Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                        AdorKd1 = cb_AdorKDinput.Text;
                        //SoCT1 = txt_SoCTinput.Text;
                        SoBB1 = txt_SoBBinput.Text.Trim();
                        SoXe1 = txt_SoXeinput.Text;
                        LoaiGo1 = cb_LoaiGoinput.Text.Trim();
                        CDai_d = double.Parse(txt_CDaiinput.Text);
                        CRong_d = double.Parse(txt_CRonginput.Text);
                        CDay_d = double.Parse(txt_CDayinput.Text);
                        SoLuong_d = double.Parse(txt_SoLuonginput.Text);
                        
                        DVT1 = cb_DVTinput.Text.Trim();
                        PhanLoai1 = cb_PLoaiinput.Text.Trim();
                        KhachHang1 = cb_KhachHanginput.Text.Trim();
                        GhiChu1 = txt_GhiChuinput.Text.Trim();
                        if (cb_MaCTinput.Text != "")
                        {
                            MaCT1 = prs.substring_right(cb_MaCTinput.Text, child);
                            TenCT1 = prs.substring_left(cb_MaCTinput.Text, child);
                        }
                        else
                        {
                            MaCT1 = "";
                            TenCT1 = "";
                        }
                        if (check_combobox(cb_LoaiGoinput, LoaiGo1) && check_combobox(cb_PLoaiinput, PhanLoai1)&& check_combobox(cb_AdorKDinput, AdorKd1))
                        {
                            if (chk_MatLa.Visible == false || chk_MatLa.Checked == false)
                            {
                                sum_temp = CDay_d * CRong_d * CDai_d * SoLuong_d / 1000000000;
                                SoKhoi_d = 0;
                            }
                            if (chk_MatLa.Checked == true&&chk_MatLa.Visible==true)
                            {
                                if (CDai_d > 10)
                                    MessageBox.Show("Có thể bạn đang nhập sai thông tin");
                                sum_temp = CDai_d;
                                SoKhoi_d = CDai_d;
                                CDai_d = 0;
                                CRong_d = 0;
                                SoLuong_d = 1;
                                
                            }
                            //Number_temp = string.Format("{0:###,###.######}", sum_temp);
                            dataGridView1.Rows.Add(NoiGiao1, NoiNhan1, AdorKd1, SoBB1, LoaiGo1, CDay_d, CRong_d, CDai_d,sum_temp, SoLuong_d, DVT1, PhanLoai1, KhachHang1, MaCT1, TenCT1, GhiChu1, Ngay1, txt_Ngayinput.Text, LyDo1, SoXe1,SoKhoi_d);
                            sum_temp += double.Parse(lb_Tonginput.Text);
                            lb_Tonginput.Text = sum_temp.ToString();
                            j = dataGridView1.Rows.Count - 1;
                            dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells[0];
                            dataGridView1.FirstDisplayedCell = dataGridView1.Rows[j].Cells[0];
                            prs.formatcolordatagrid(dataGridView1);
                            txt_CDayinput.Text = "";
                            //txt_CRonginput.Text = "";
                            txt_CDaiinput.Text = "";
                            txt_SoLuonginput.Text = "";
                            cb_MaCTinput.Text = "";
                            txt_GhiChuinput.Text = "";
                            temp.Clone();
                            cb_QuyCachinput.Focus();
                        }
                        else
                            MessageBox.Show("Kiểm tra lại thông tin nhập ở các combobox");

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            add_grid();
        }
        private void save()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            ChungTu1 = "0";
            try
            {
                if (cb_NoiGiaoinput.Text == "" || cb_NoiNhaninput.Text == "" || cb_LoaiGoinput.Text == ""||cb_LyDoinput.Text=="")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaoinput.Text, child);
                    NoiNhan1 = prs.substring_right(cb_NoiNhaninput.Text, child);
                    LyDo1 = prs.substring_right(cb_LyDoinput.Text, child);
                    if (NoiGiao1 == "" || NoiNhan1 == ""||LyDo1=="")
                        MessageBox.Show("Nhập sai thông tin");
                    else
                    {
                        if (txt_SoCTinput.Enabled == false && txt_SoCTinput.Text == "")
                        {
                            LoaiGo1 = cb_LoaiGoinput.Text.Trim();
                            AdorKd1 = cb_AdorKDinput.Text.Trim();
                            string[] temp = txt_Ngayinput.Text.Split('/');
                            Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                            SoBB1 = txt_SoBBinput.Text.Trim();
                            cmd.CommandText = "Ins_KhoGoCT";
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            SqlParameter code = new SqlParameter("@MaCT", SqlDbType.Int);
                            code.Direction = ParameterDirection.Output;
                            cmd.Parameters.Add(code);
                            SqlDataReader sdr = cmd.ExecuteReader();
                            ChungTu1 = cmd.Parameters["@MaCT"].Value.ToString();
                            sdr.Close();
                            cmd.Parameters.Clear();
                        }
                        if (txt_SoCTinput.Enabled == true && txt_SoCTinput.Text != "")
                            ChungTu1 = txt_SoCTinput.Text.Trim();
                        if (ChungTu1 != "0")
                        {
                            cmd.CommandText = "Ins_KhoGo";
                            for (i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                            {
                                NoiGiao1 = dataGridView1.Rows[i].Cells["NoiGiaoinput"].Value.ToString();
                                NoiNhan1 = dataGridView1.Rows[i].Cells["NoiNhaninput"].Value.ToString();
                                LyDo1 = dataGridView1.Rows[i].Cells["LyDoinput"].Value.ToString();
                                LoaiGo1 = dataGridView1.Rows[i].Cells["LoaiGoinput"].Value.ToString();
                                AdorKd1 = dataGridView1.Rows[i].Cells["ADorKDinput"].Value.ToString();
                                SoXe1 = dataGridView1.Rows[i].Cells["SoXeinput"].Value.ToString();
                                SoBB1 = dataGridView1.Rows[i].Cells["SoBBinput"].Value.ToString();
                                CDay1 = dataGridView1.Rows[i].Cells["CDayinput"].Value.ToString();
                                CRong1 = dataGridView1.Rows[i].Cells["CRonginput"].Value.ToString();
                                CDai1 = dataGridView1.Rows[i].Cells["CDaiinput"].Value.ToString();
                                SoKhoi1 = dataGridView1.Rows[i].Cells["SoKhoiInsertInput"].Value.ToString();
                                SoLuong1 = dataGridView1.Rows[i].Cells["SoLuonginput"].Value.ToString();
                                DVT1 = dataGridView1.Rows[i].Cells["DVTinput"].Value.ToString();
                                PhanLoai1 = dataGridView1.Rows[i].Cells["PhanLoaiinput"].Value.ToString();
                                KhachHang1 = dataGridView1.Rows[i].Cells["KhachHanginput"].Value.ToString();
                                MaCT1 = dataGridView1.Rows[i].Cells["TenCTinput"].Value.ToString();
                                GhiChu1 = dataGridView1.Rows[i].Cells["GhiChuinput"].Value.ToString();
                                Ngay1 = dataGridView1.Rows[i].Cells["Ngayinput"].Value.ToString();
                                cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                                cmd.Parameters.AddWithValue("@SoBB", SoBB1);
                                cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                                cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                                cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                                cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                                cmd.Parameters.AddWithValue("@ADorKD", AdorKd1);
                                cmd.Parameters.AddWithValue("@SoXe", SoXe1);
                                cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                cmd.Parameters.AddWithValue("@ChieuDay", CDay1);
                                cmd.Parameters.AddWithValue("@ChieuRong", CRong1);
                                cmd.Parameters.AddWithValue("@ChieuDai", CDai1);
                                cmd.Parameters.AddWithValue("@SoKhoi", SoKhoi1);
                                cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                                cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@MaCT", MaCT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@NguonGo", "");
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                                cmd.Parameters.AddWithValue("@UsersInsert", lb_user.Text);
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                dataGridView1.Rows.RemoveAt(i);
                                

                            }
                            MessageBox.Show("Dữ liệu được lưu thành công");
                            bt_reset_Click(null, null);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
            
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            save();
        }

        private void bt_reset_Click(object sender, EventArgs e)
        {
            txt_CDayinput.Text = "";
            txt_CRonginput.Text = "";
            txt_CDaiinput.Text = "";
            txt_SoLuonginput.Text = "";
            cb_MaCTinput.Text = "";
            txt_GhiChuinput.Text = "";
            
            txt_SoCTinput.Text = "";
            txt_SoCTinput.Enabled = false;
            cb_AdorKDinput.Text = "";
            cb_LyDoinput.Text = "";
            cb_NoiGiaoinput.Text = "";
            cb_LyDoinput.Enabled = true;
            cb_NoiNhaninput.Enabled = true;
            cb_NoiGiaoinput.Enabled = true;
            cb_AdorKDinput.Enabled = true;
            cb_LoaiGoinput.Enabled = true;
            bt_insertinput.Enabled = true;
            bt_deleteinput.Enabled = false;
            bt_editinput.Enabled = false;
            txt_Serialinput.Text = "";
            txt_Serialinput.Enabled = false;
            lb_Tonginput.Text = "0";
            if (dataGridView2.Rows.Count > 1)
            {
                for (i = dataGridView2.Rows.Count - 2; i >= 0; i--)
                {
                    dataGridView2.Rows.RemoveAt(i);
                }
            }
            txt_CDayinput.Focus();
            
        }
        private void Display_Input(int rows)
        {
            cb_NoiGiaoinput.Text = dataGridView2.Rows[rows].Cells["NoiGiao"].Value.ToString();
            cb_AdorKDinput.Text = dataGridView2.Rows[rows].Cells["ADorKD"].Value.ToString();
            cb_LoaiGoinput.Text = dataGridView2.Rows[rows].Cells["LoaiGo"].Value.ToString();
            cb_NoiNhaninput.Text = dataGridView2.Rows[rows].Cells["NoiNhan"].Value.ToString();
            cb_LyDoinput.Text = dataGridView2.Rows[rows].Cells["LyDo"].Value.ToString();
            txt_SoBBinput.Text = dataGridView2.Rows[rows].Cells["SoBB"].Value.ToString();
            txt_SoCTinput.Text = dataGridView2.Rows[rows].Cells["ChungTu"].Value.ToString();
            txt_Ngayinput.Text = dataGridView2.Rows[rows].Cells["Ngay"].Value.ToString();
            txt_CDayinput.Text = dataGridView2.Rows[rows].Cells["ChieuDay"].Value.ToString();
            txt_CRonginput.Text = dataGridView2.Rows[rows].Cells["ChieuRong"].Value.ToString();
            txt_CDaiinput.Text = (double.Parse(dataGridView2.Rows[rows].Cells["ChieuDai"].Value.ToString()) + double.Parse(dataGridView2.Rows[rows].Cells["SoKhoi"].Value.ToString())).ToString(); ;
            txt_SoLuonginput.Text = dataGridView2.Rows[rows].Cells["SLNhap"].Value.ToString();
            cb_DVTinput.Text = dataGridView2.Rows[rows].Cells["DVT"].Value.ToString();
            cb_PLoaiinput.Text = dataGridView2.Rows[rows].Cells["PhanLoai"].Value.ToString();
            cb_KhachHanginput.Text = dataGridView2.Rows[rows].Cells["KhachHang"].Value.ToString();
            cb_MaCTinput.Text = dataGridView2.Rows[rows].Cells["MaCT"].Value.ToString();
            txt_GhiChuinput.Text = dataGridView2.Rows[rows].Cells["GhiChu"].Value.ToString();
            txt_Serialinput.Text = dataGridView2.Rows[rows].Cells["Serial"].Value.ToString();
            txt_Serialinput.Enabled = false;
        }
        private void search()
        {
            try
            {
                string search = null;
                Process prs = new Process();
                DataSet ds = new DataSet();
                if (txt_Serialinput.Text == "")
                {
                        
                        string[] temp = txt_Ngayinput.Text.Split('/');
                        Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];

                        search = @"SELECT     dbo.KhoGo.Serial, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai, dbo.KhoGo.SLNhap, 
                      dbo.KhoGo.SLXuat, dbo.KhoGo.DVT, dbo.KhoGo.MaCT, dbo.KhoGo.KhachHang, dbo.KhoGo.GhiChu, dbo.KhoGo.Ngay, dbo.KhoGo.LoaiGo, 
                      dbo.KhoGo.LyDo, dbo.KhoGo.NoiGiao, dbo.KhoGo.NoiNhan, dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,dbo.KhoGo.SoKhoi
                        FROM         dbo.KhoGo where SLNhap>0 and dbo.KhoGo.Ngay='" + Ngay1 + "'";
                   
                }
                else
                {

                    search = @"SELECT     dbo.KhoGo.Serial, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai, dbo.KhoGo.SLNhap, 
                      dbo.KhoGo.SLXuat, dbo.KhoGo.DVT, dbo.KhoGo.MaCT, dbo.KhoGo.KhachHang, dbo.KhoGo.GhiChu, dbo.KhoGo.Ngay, dbo.KhoGo.LoaiGo, 
                      dbo.KhoGo.LyDo, dbo.KhoGo.NoiGiao, dbo.KhoGo.NoiNhan, dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,dbo.KhoGo.SoKhoi
                        FROM         dbo.KhoGo where dbo.KhoGo.Serial=" + txt_Serialinput.Text;
                }
                //MessageBox.Show(search);
                ds = prs.dts(search);
                if (ds.Tables[0].Rows.Count == 0)
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra lại thông tin tìm kiếm");
                else
                {
                    dataGridView2.DataSource = ds.Tables[0];
                    int k = dataGridView2.Rows.Count - 2;
                    Display_Input(k);
                    lb_index.Text = k.ToString();
                    bt_editinput.Enabled = true;
                    bt_deleteinput.Enabled = true;
                    bt_insertinput.Enabled = false;
                    cb_LyDoinput.Enabled = true;
                    cb_NoiNhaninput.Enabled = true;
                    cb_NoiGiaoinput.Enabled = true;
                    cb_AdorKDinput.Enabled = true;
                    cb_LoaiGoinput.Enabled = true;
                    txt_SoCTinput.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void edit()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            Conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Upd_KhoGo";
            try
            {
                if (cb_NoiGiaoinput.Text == "" || cb_NoiNhaninput.Text == "" || cb_LoaiGoinput.Text == "" || txt_CDayinput.Text == "" || txt_CRonginput.Text == "" || txt_CDaiinput.Text == "" || txt_SoLuonginput.Text == "" || cb_PLoaiinput.Text == "" || txt_SoBBinput.Text == "" || cb_LyDoinput.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaoinput.Text, child);
                    NoiNhan1 = prs.substring_right(cb_NoiNhaninput.Text, child);
                    LyDo1 = prs.substring_right(cb_LyDoinput.Text, child);
                    if (NoiGiao1 == "")
                        NoiGiao1 = cb_NoiGiaoinput.Text.Trim();
                    if (NoiNhan1 == "")
                        NoiNhan1 = cb_NoiNhaninput.Text.Trim();
                    if (LyDo1 == "")
                        LyDo1 = cb_LyDoinput.Text.Trim();

                    string[] temp = txt_Ngayinput.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    AdorKd1 = cb_AdorKDinput.Text;
                    SoCT1 = txt_SoCTinput.Text;
                    SoBB1 = txt_SoBBinput.Text.Trim();
                    SoXe1 = txt_SoXeinput.Text;
                    LoaiGo1 = cb_LoaiGoinput.Text.Trim();
                    CDai_d = double.Parse(txt_CDaiinput.Text);
                    CRong_d = double.Parse(txt_CRonginput.Text);
                    CDay_d = double.Parse(txt_CDayinput.Text);
                    SoLuong_d = double.Parse(txt_SoLuonginput.Text);
                    DVT1 = cb_DVTinput.Text.Trim();
                    PhanLoai1 = cb_PLoaiinput.Text.Trim();
                    KhachHang1 = cb_KhachHanginput.Text.Trim();
                    GhiChu1 = txt_GhiChuinput.Text.Trim();
                    TenCT1 = "";

                    if (cb_MaCTinput.Text != "")
                    {
                        
                        TenCT1 = prs.substring_left(cb_MaCTinput.Text, child);
                    }
                   if(TenCT1=="")
                    {
                        TenCT1 = cb_MaCTinput.Text;
                    }

                    DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin có Số Serial " + txt_Serialinput.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
                        string_temp=txt_Serialinput.Text;
                        for (i = dataGridView2.Rows.Count-2; i >=0; i--)
                        {
                            if (string_temp == dataGridView2.Rows[i].Cells["Serial"].Value.ToString())
                            {
                                if (dataGridView2.Rows[i].Cells["SoKhoi"].Value.ToString() == "0")
                                {
                                    cmd.Parameters.AddWithValue("@Serial", txt_Serialinput.Text);
                                    cmd.Parameters.AddWithValue("@SoBB", SoBB1);
                                    cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                                    cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                                    cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                                    cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                                    cmd.Parameters.AddWithValue("@ADorKD", AdorKd1);
                                    cmd.Parameters.AddWithValue("@SoXe", SoXe1);
                                    cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                    cmd.Parameters.AddWithValue("@ChieuDay", CDay_d);
                                    cmd.Parameters.AddWithValue("@ChieuRong", CRong_d);
                                    cmd.Parameters.AddWithValue("@ChieuDai", CDai_d);
                                    cmd.Parameters.AddWithValue("@SLNhap", SoLuong_d);
                                    cmd.Parameters.AddWithValue("@SLXuat", "0");
                                    cmd.Parameters.AddWithValue("@DVT", DVT1);
                                    cmd.Parameters.AddWithValue("@MaCT", TenCT1);
                                    cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                    cmd.Parameters.AddWithValue("@NguonGo", "");
                                    cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                                    cmd.Parameters.AddWithValue("@UsersUpdate", lb_user.Text);
                                    cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                    cmd.ExecuteNonQuery();
                                    cmd.Parameters.Clear();
                                    MessageBox.Show("Chỉnh sửa thành công");
                                    bt_reset_Click(null, null);
                                   
                                }
                                else
                                    MessageBox.Show("Dữ liệu thuộc nhóm Mặt Lã, không được sửa. Bạn có thể xóa, sau đó thêm mới");
                                break;
                            }
                        }
                       
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txt_Serialinput.Visible = true;
            txt_Serialinput.Enabled = true;
        }

        private void bt_previnput_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView2.Rows.Count > 1)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_Input(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_nextinput_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView2.Rows.Count > 1)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView2.Rows.Count - 2)
                    {
                        i++;
                        Display_Input(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_searchinput_Click(object sender, EventArgs e)
        {
            search();
        }

        private void bt_editinput_Click(object sender, EventArgs e)
        {
            edit();
        }
        private void delete()
        {
            DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa Thông Tin có Số Serial " + txt_Serialinput.Text, "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                try
                {
                    string datecheck = @"select datediff(dd,NgayInsert,getdate()) from dbo.KhoGo where Serial='" + txt_Serialinput.Text + "'";
                    //string delete = @"delete from dbo.KhoTP_NK where Serial='" + lb_serialinput.Text + "'";
                    Data dta = new Data();
                    SqlConnection Conn = dta.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;
                    cmd.CommandText = datecheck;
                    int date_count = int.Parse(cmd.ExecuteScalar().ToString());
                    if (date_count <= 1)
                    {
                        cmd.CommandText = "delete from dbo.KhoGo where Serial=" + txt_Serialinput.Text;
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Dữ liệu được xóa thành công");

                        txt_Serialinput.Text = "0";
                    }
                    else
                    {
                        MessageBox.Show("Quá thời gian cho phép,Không được phép xóa");
                    }
                    Conn.Close();
                    cmd.Dispose();
                    for (int i = dataGridView2.Rows.Count - 2; i >= 0; i--)
                    {
                        dataGridView2.Rows.RemoveAt(i);
                    }

                    bt_reset_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void bt_deleteinput_Click(object sender, EventArgs e)
        {
            delete();
        }

        /*private void chk_QuyDoi_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_QuyDoi.Checked == true)
            {
                txt_CRonginput.Text = "1";
                txt_CDayinput.Text = "1";
                txt_CDayinput.Enabled = false;
                txt_CRonginput.Enabled = false;
                lb_CDaiinput.Text = "Khối";
            }
            else
            {
                txt_CRonginput.Text = "";
                txt_CDayinput.Text = "";
                txt_CDayinput.Enabled = true;
                txt_CDayinput.Enabled = true;
                lb_CDaiinput.Text = "Dài";
            }

        }
       */

        private void print_input()
        {
            try
            {
                if (txt_Ngayinput.Text != "")
                {
                    Process prs = new Process();
                    Ngay1 = txt_Ngayinput.Text;
                    string[] temp = Ngay1.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    string_temp = "0";
                    string print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLNhap,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLNhap)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH, 
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLNhap>0 and dbo.KhoGo.Ngay='" + Ngay1 + "'";

                    if (txt_CDayinput.Text != "")
                    {
                        print_NKTG += @" and dbo.KhoGo.ChieuDay=" + txt_CDayinput.Text;
                    }
                    if (txt_CDayinput.Text != "" && txt_CRonginput.Text != "" && txt_CDaiinput.Text != "")
                    {
                        print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLNhap,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLNhap)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH, 
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLNhap>0 and dbo.KhoGo.ChieuDay=" + txt_CDayinput.Text + " and dbo.KhoGo.ChieuRong=" + txt_CRonginput.Text + " and dbo.KhoGo.ChieuDai=" + double.Parse(txt_CDaiinput.Text).ToString() + "  and dbo.KhoGo.Ngay='" + Ngay1 + "'";
                    }
                    if (txt_SoBBinput.Text != "")
                    {
                        print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLNhap,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLNhap)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH, 
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLNhap>0 and dbo.KhoGo.SoBB='" + txt_SoBBinput.Text + "'";
                        string_temp = "1";
                    }
                    if (txt_SoCTinput.Text != "")
                    {
                        print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLNhap,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLNhap)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH, 
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLNhap>0 and dbo.KhoGo.ChungTu='" + txt_SoCTinput.Text + "'";
                        string_temp = "2";
                    }
                    DataSet ds = prs.dts(print_NKTG);
                    Fr_Report FrR = new Fr_Report();
                    if(string_temp=="0")
                        Ngay1 = "Ngày nhập: "+txt_Ngayinput.Text;
                    if (string_temp == "1")
                        Ngay1 = "Số BB: " + txt_SoBBinput.Text;
                    if (string_temp == "2")
                        Ngay1 = "Số CT: " + txt_SoCTinput.Text;
                    FrR.report_KhoGo_Nhap(ds, Ngay1);
                    FrR.Show();
                    ds.Clear();

                }
                else
                    MessageBox.Show("Chọn ngày cần in");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_printinput_Click(object sender, EventArgs e)
        {
            print_input();
        }

        private void Fr_KhoGo_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F11)
            {
                if (dataGridView1.Rows.Count > 1)
                {
                    bt_save_Click(null, null);
                }
            }
            if (ex.KeyCode == Keys.F12)
            {
                if (dataGridView3.Rows.Count > 1)
                {
                    bt_saveoutput_Click(null, null);
                }
            }
           
            if (ex.KeyCode == Keys.End)
            {
                if (tab_control.SelectedTab == tab_Nhap)
                {
                    if (bt_insertinput.Enabled == true)
                        bt_insert_Click(null, null);
                    else
                        MessageBox.Show("Chức năng này chưa được kích hoạt, Nhấn nút reset để kích hoạt lại");
                }
                if (tab_control.SelectedTab == tab_Xuat)
                {
                    if (bt_insertoutput.Enabled == true)
                        bt_insertoutput_Click(null, null);
                    else
                        MessageBox.Show("Chức năng này chưa được kích hoạt, Nhấn nút reset để kích hoạt lại");
                }
            }
        }

        private void add_gridoutput()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                if (cb_NoiGiaooutput.Text == "" || cb_NoiNhanoutput.Text == "" || cb_LoaiGooutput.Text == "" || txt_CDayoutput.Text == "" || txt_CRongoutput.Text == "" || txt_CDaioutput.Text == "" || txt_SoLuongoutput.Text == "" || cb_PLoaioutput.Text == "" || txt_SoBBoutput.Text == "" || cb_LyDooutput.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaooutput.Text, child).Trim();
                    NoiNhan1 = prs.substring_right(cb_NoiNhanoutput.Text, child).Trim();
                    LyDo1 = prs.substring_right(cb_LyDooutput.Text, child).Trim();
                    if (NoiGiao1 == "" || NoiNhan1 == "" || LyDo1 == "")
                        MessageBox.Show("Nhập sai thông tin");
                    else
                    {

                        string[] temp = txt_Ngayoutput.Text.Split('/');
                        Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                        AdorKd1 = cb_AdorKDoutput.Text;
                        SoBB1 = txt_SoBBoutput.Text.Trim();
                        SoXe1 = "";
                        LoaiGo1 = cb_LoaiGooutput.Text.Trim();
                        CDai_d = double.Parse(txt_CDaioutput.Text);
                        CRong_d = double.Parse(txt_CRongoutput.Text);
                        CDay_d = double.Parse(txt_CDayoutput.Text);
                        SoLuong_d = double.Parse(txt_SoLuongoutput.Text);
                        DVT1 = cb_DVToutput.Text.Trim();
                        PhanLoai1 = cb_PLoaioutput.Text.Trim();
                        KhachHang1 = cb_KhachHangoutput.Text.Trim();
                       
                        if (cb_MaCToutput.Text != "")
                        {
                            MaCT1 = prs.substring_right(cb_MaCToutput.Text, child);
                            TenCT1 = prs.substring_left(cb_MaCToutput.Text, child);
                        }
                        else
                        {
                            MaCT1 = "";
                            TenCT1 = "";
                        }

                        if (cb_NguonGooutput.Text != "")
                            NguonGo1 = prs.substring_right(cb_NguonGooutput.Text, child);
                        else
                            NguonGo1 = "";
                        check = 0;
                        if (dataGridView3.Rows.Count > 1)
                        {
                            GhiChu1 = txt_CDayoutput.Text + txt_CRongoutput.Text + double.Parse(txt_CDaioutput.Text).ToString();
                            for(i = dataGridView3.Rows.Count - 2; i >= 0; i--)
                            {
                                string_temp = dataGridView3.Rows[i].Cells["CDayoutput"].Value.ToString() + dataGridView3.Rows[i].Cells["CRongoutput"].Value.ToString() + dataGridView3.Rows[i].Cells["CDaioutput"].Value.ToString();
                                if (GhiChu1 == string_temp)
                                {
                                    check = 1;
                                    break;
                                }
                            }

                        }
                        if (check == 0)
                        {
                            GhiChu1 = txt_GhiChuoutput.Text.Trim();
                            if (check_combobox(cb_LoaiGooutput, LoaiGo1) && check_combobox(cb_PLoaioutput, PhanLoai1) && check_combobox(cb_AdorKDoutput, AdorKd1))
                            {
                                if (chk_MatLaoutput.Visible == false || chk_MatLaoutput.Checked == false)
                                {
                                    sum_temp = CDay_d * CRong_d * CDai_d * SoLuong_d / 1000000000;
                                    SoKhoi_d = 0;
                                }
                                if (chk_MatLaoutput.Visible == true && chk_MatLaoutput.Checked == true)
                                {
                                    if (CDai_d > 10)
                                        MessageBox.Show("Có thể bạn đang nhập sai thông tin");
                                    sum_temp = CDai_d;
                                    SoKhoi_d = CDai_d;
                                    CDai_d = 0;
                                    CRong_d = 0;
                                    SoLuong_d = 1;

                                }
                                dataGridView3.Rows.Add(NoiGiao1, NoiNhan1, AdorKd1, SoBB1, LoaiGo1, CDay_d, CRong_d, CDai_d,sum_temp, SoLuong_d, DVT1, PhanLoai1, KhachHang1, NguonGo1, MaCT1, TenCT1, GhiChu1, Ngay1, txt_Ngayoutput.Text, LyDo1,SoKhoi_d);
                               
                                sum_temp += double.Parse(lb_Tongoutput.Text);
                                lb_Tongoutput.Text = sum_temp.ToString();
                                j = dataGridView3.Rows.Count - 1;
                                dataGridView3.CurrentCell = dataGridView3.Rows[j].Cells[0];
                                dataGridView3.FirstDisplayedCell = dataGridView3.Rows[j].Cells[0];
                                prs.formatcolordatagrid(dataGridView3);
                                txt_CDayoutput.Text = "";
                                //txt_CRongoutput.Text = "";
                                txt_CDaioutput.Text = "";
                                txt_SoLuongoutput.Text = "";
                                cb_MaCToutput.Text = "";
                                txt_GhiChuoutput.Text = "";
                                temp.Clone();
                                cb_NguonGooutput.Text = "";
                                cb_QuyCachoutput.Focus();
                            }
                            else
                                MessageBox.Show("Kiểm tra lại thông tin nhập ở combobox");

                        }
                        else
                            MessageBox.Show("Quy cách này đã có trong DS phía dưới");

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void bt_insertoutput_Click(object sender, EventArgs e)
        {
            add_gridoutput();
        }

        private void save_output()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            ChungTu1 = "0";
            try
            {
                if (cb_NoiGiaooutput.Text == "" || cb_NoiNhanoutput.Text == "" || cb_LoaiGooutput.Text == "" || cb_LyDooutput.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaooutput.Text, child);
                    NoiNhan1 = prs.substring_right(cb_NoiNhanoutput.Text, child);
                    LyDo1 = prs.substring_right(cb_LyDooutput.Text, child);
                    if (NoiGiao1 == "" || NoiNhan1 == "" || LyDo1 == "")
                        MessageBox.Show("Nhập sai thông tin");
                    else
                    {
                        if (txt_SoCToutput.Enabled == false && txt_SoCToutput.Text == "")
                        {
                            LoaiGo1 = cb_LoaiGooutput.Text.Trim();
                            AdorKd1 = cb_AdorKDoutput.Text.Trim();
                            string[] temp = txt_Ngayoutput.Text.Split('/');
                            Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                            SoBB1 = txt_SoBBoutput.Text.Trim();
                            cmd.CommandText = "Ins_KhoGoCT";
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            SqlParameter code = new SqlParameter("@MaCT", SqlDbType.Int);
                            code.Direction = ParameterDirection.Output;
                            cmd.Parameters.Add(code);
                            SqlDataReader sdr = cmd.ExecuteReader();
                            ChungTu1 = cmd.Parameters["@MaCT"].Value.ToString();
                            sdr.Close();
                            cmd.Parameters.Clear();
                        }
                        if (txt_SoCToutput.Enabled == true && txt_SoCToutput.Text != "")
                            ChungTu1 = txt_SoCToutput.Text.Trim();
                        if (ChungTu1 != "0")
                        {
                            cmd.CommandText = "Ins_KhoGo";
                            for (i = dataGridView3.Rows.Count - 2; i >= 0; i--)
                            {
                                NoiGiao1 = dataGridView3.Rows[i].Cells["NoiGiaooutput"].Value.ToString();
                                NoiNhan1 = dataGridView3.Rows[i].Cells["NoiNhanoutput"].Value.ToString();
                                LyDo1 = dataGridView3.Rows[i].Cells["LyDooutput"].Value.ToString();
                                LoaiGo1 = dataGridView3.Rows[i].Cells["LoaiGooutput"].Value.ToString();
                                AdorKd1 = dataGridView3.Rows[i].Cells["ADorKDoutput"].Value.ToString();
                               // SoXe1 = dataGridView3.Rows[i].Cells["SoXeoutput"].Value.ToString();
                                SoBB1 = dataGridView3.Rows[i].Cells["SoBBoutput"].Value.ToString();
                                CDay1 = dataGridView3.Rows[i].Cells["CDayoutput"].Value.ToString();
                                CRong1 = dataGridView3.Rows[i].Cells["CRongoutput"].Value.ToString();
                                CDai1 = dataGridView3.Rows[i].Cells["CDaioutput"].Value.ToString();
                                SoKhoi1 = dataGridView3.Rows[i].Cells["SoKhoiInsertOutput"].Value.ToString();
                                SoLuong1 = dataGridView3.Rows[i].Cells["SoLuongoutput"].Value.ToString();
                                DVT1 = dataGridView3.Rows[i].Cells["DVToutput"].Value.ToString();
                                PhanLoai1 = dataGridView3.Rows[i].Cells["PhanLoaioutput"].Value.ToString();
                                KhachHang1 = dataGridView3.Rows[i].Cells["KhachHangoutput"].Value.ToString();
                                MaCT1 = dataGridView3.Rows[i].Cells["TenCToutput"].Value.ToString();
                                GhiChu1 = dataGridView3.Rows[i].Cells["GhiChuoutput"].Value.ToString();
                                Ngay1 = dataGridView3.Rows[i].Cells["Ngayoutput"].Value.ToString();
                                NguonGo1 = dataGridView3.Rows[i].Cells["NguonGo"].Value.ToString();
                                cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                                cmd.Parameters.AddWithValue("@SoBB", SoBB1);
                                cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                                cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                                cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                                cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                                cmd.Parameters.AddWithValue("@ADorKD", AdorKd1);
                                cmd.Parameters.AddWithValue("@SoXe", "");
                                cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                cmd.Parameters.AddWithValue("@ChieuDay", CDay1);
                                cmd.Parameters.AddWithValue("@ChieuRong", CRong1);
                                cmd.Parameters.AddWithValue("@ChieuDai", CDai1);
                                cmd.Parameters.AddWithValue("@SoKhoi", SoKhoi1);
                                cmd.Parameters.AddWithValue("@SLNhap", "0");
                                cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@MaCT", MaCT1);
                                cmd.Parameters.AddWithValue("@NguonGo", NguonGo1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                               
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                                cmd.Parameters.AddWithValue("@UsersInsert", lb_user.Text);
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                dataGridView3.Rows.RemoveAt(i);
                            }
                            MessageBox.Show("Dữ liệu được lưu thành công");
                            bt_resertoutput_Click(null, null);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_saveoutput_Click(object sender, EventArgs e)
        {
            save_output();
        }

        private void bt_resertoutput_Click(object sender, EventArgs e)
        {
            txt_CDayoutput.Text = "";
            txt_CRongoutput.Text = "";
            txt_CDaioutput.Text = "";
            txt_SoLuongoutput.Text = "";
            cb_MaCToutput.Text = "";
            txt_GhiChuoutput.Text = "";

            txt_SoCToutput.Text = "";
            txt_SoCToutput.Enabled = false;
            cb_AdorKDoutput.Text = "";
            cb_LyDooutput.Text = "";
            cb_NoiGiaooutput.Text = "";
            cb_LyDooutput.Enabled = true;
            cb_NoiNhanoutput.Enabled = true;
            cb_NoiGiaooutput.Enabled = true;
            cb_AdorKDoutput.Enabled = true;
            cb_LoaiGooutput.Enabled = true;
            bt_insertoutput.Enabled = true;
            bt_deleteoutput.Enabled = false;
            bt_editoutput.Enabled = false;
            txt_Serialoutput.Text = "";
            txt_Serialoutput.Enabled = false;
            cb_NguonGooutput.Text = "";
            lb_TonKho.Text = "0";
            lb_KhoiTK.Text = "0";
            cb_QuyCachoutput.Text = "";
            lb_Tongoutput.Text = "0";
            cb_NoiGiaooutput.Focus();

            if (dataGridView4.Rows.Count > 1)
            {
                for (i = dataGridView4.Rows.Count - 2; i >= 0; i--)
                {
                    dataGridView4.Rows.RemoveAt(i);
                }
            }
            cb_NoiGiaooutput.Focus();

        }
        private void Display_output(int rows)
        {
            cb_NoiGiaooutput.Text = dataGridView4.Rows[rows].Cells["NoiGiao"].Value.ToString();
            cb_AdorKDoutput.Text = dataGridView4.Rows[rows].Cells["ADorKD"].Value.ToString();
            cb_LoaiGooutput.Text = dataGridView4.Rows[rows].Cells["LoaiGo"].Value.ToString();
            cb_NoiNhanoutput.Text = dataGridView4.Rows[rows].Cells["NoiNhan"].Value.ToString();
            cb_LyDooutput.Text = dataGridView4.Rows[rows].Cells["LyDo"].Value.ToString();
            txt_SoBBoutput.Text = dataGridView4.Rows[rows].Cells["SoBB"].Value.ToString();
            txt_SoCToutput.Text = dataGridView4.Rows[rows].Cells["ChungTu"].Value.ToString();
            txt_Ngayoutput.Text = dataGridView4.Rows[rows].Cells["Ngay"].Value.ToString();
            txt_CDayoutput.Text = dataGridView4.Rows[rows].Cells["ChieuDay"].Value.ToString();
            txt_CRongoutput.Text = dataGridView4.Rows[rows].Cells["ChieuRong"].Value.ToString();
            txt_CDaioutput.Text = (double.Parse(dataGridView4.Rows[rows].Cells["ChieuDai"].Value.ToString())+double.Parse(dataGridView4.Rows[rows].Cells["SoKhoi"].Value.ToString())).ToString();

            txt_SoLuongoutput.Text = dataGridView4.Rows[rows].Cells["SLXuat"].Value.ToString();
            cb_DVToutput.Text = dataGridView4.Rows[rows].Cells["DVT"].Value.ToString();
            cb_PLoaioutput.Text = dataGridView4.Rows[rows].Cells["PhanLoai"].Value.ToString();
            cb_KhachHangoutput.Text = dataGridView4.Rows[rows].Cells["KhachHang"].Value.ToString();
            cb_MaCToutput.Text = dataGridView4.Rows[rows].Cells["MaCT"].Value.ToString();
            txt_GhiChuoutput.Text = dataGridView4.Rows[rows].Cells["GhiChu"].Value.ToString();
            txt_Serialoutput.Text = dataGridView4.Rows[rows].Cells["Serial"].Value.ToString();
            cb_NguonGooutput.Text = dataGridView4.Rows[rows].Cells["NguonGo"].Value.ToString();
            txt_Serialoutput.Enabled = false;

        }
        private void search_out()
        {

            try
            {
                string search = null;
                Process prs = new Process();
                DataSet ds = new DataSet();
                if (txt_Serialoutput.Text == "")
                {

                    string[] temp = txt_Ngayoutput.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];

                    search = @"SELECT     dbo.KhoGo.Serial, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai,
                      dbo.KhoGo.SLXuat, dbo.KhoGo.DVT, dbo.KhoGo.MaCT, dbo.KhoGo.KhachHang, dbo.KhoGo.GhiChu, dbo.KhoGo.Ngay, dbo.KhoGo.LoaiGo, KhoGo.NguonGo,
                      dbo.KhoGo.LyDo, dbo.KhoGo.NoiGiao, dbo.KhoGo.NoiNhan, dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,dbo.KhoGo.SoKhoi
                        FROM         dbo.KhoGo where SLXuat>0 and dbo.KhoGo.Ngay='" + Ngay1 + "'";

                }
                else
                {

                    search = @"SELECT     dbo.KhoGo.Serial, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai,
                      dbo.KhoGo.SLXuat, dbo.KhoGo.DVT, dbo.KhoGo.MaCT, dbo.KhoGo.KhachHang, dbo.KhoGo.GhiChu, dbo.KhoGo.Ngay, dbo.KhoGo.LoaiGo, KhoGo.NguonGo,
                      dbo.KhoGo.LyDo, dbo.KhoGo.NoiGiao, dbo.KhoGo.NoiNhan, dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,dbo.KhoGo.SoKhoi
                        FROM         dbo.KhoGo  where dbo.KhoGo.Serial=" + txt_Serialoutput.Text;
                }
                //MessageBox.Show(search);
                ds = prs.dts(search);
                if (ds.Tables[0].Rows.Count == 0)
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra lại thông tin tìm kiếm");
                else
                {
                    dataGridView4.DataSource = ds.Tables[0];
                    int k = dataGridView4.Rows.Count - 2;
                    Display_output(k);
                    lb_index.Text = k.ToString();
                    bt_editoutput.Enabled = true;
                    bt_deleteoutput.Enabled = true;
                    bt_insertoutput.Enabled = false;
                    /*cb_LyDooutput.Enabled = false;
                    cb_NoiNhanoutput.Enabled = false;
                    cb_NoiGiaooutput.Enabled = false;
                    cb_AdorKDoutput.Enabled = false;
                    cb_LoaiGooutput.Enabled = false;*/
                    txt_SoCToutput.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void edit_output()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            Conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Upd_KhoGo";
            try
            {
                if (cb_NoiGiaooutput.Text == "" || cb_NoiNhanoutput.Text == "" || cb_LoaiGooutput.Text == "" || txt_CDayoutput.Text == "" || txt_CRongoutput.Text == "" || txt_CDaioutput.Text == "" || txt_SoLuongoutput.Text == "" || cb_PLoaioutput.Text == "" || txt_SoBBoutput.Text == "" || cb_LyDooutput.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaooutput.Text, child);
                    NoiNhan1 = prs.substring_right(cb_NoiNhanoutput.Text, child);
                    LyDo1 = prs.substring_right(cb_LyDooutput.Text, child);
                    if (NoiGiao1 == "")
                        NoiGiao1 = cb_NoiGiaooutput.Text.Trim();
                    if (NoiNhan1 == "")
                        NoiNhan1 = cb_NoiNhanoutput.Text.Trim();
                    if (LyDo1 == "")
                        LyDo1 = cb_LyDooutput.Text.Trim();

                    string[] temp = txt_Ngayoutput.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    AdorKd1 = cb_AdorKDoutput.Text;
                    SoCT1 = txt_SoCToutput.Text;
                    SoBB1 = txt_SoBBoutput.Text.Trim();
                    //SoXe1 = txt_SoXeoutput.Text;
                    if (cb_NguonGooutput.Text != "")
                        NguonGo1 = prs.substring_right(cb_NguonGooutput.Text, child);
                    else
                        NguonGo1 = "";
                    LoaiGo1 = cb_LoaiGooutput.Text.Trim();
                    CDai_d = double.Parse(txt_CDaioutput.Text);
                    CRong_d = double.Parse(txt_CRongoutput.Text);
                    CDay_d = double.Parse(txt_CDayoutput.Text);
                    SoLuong_d = double.Parse(txt_SoLuongoutput.Text);
                    DVT1 = cb_DVToutput.Text.Trim();
                    PhanLoai1 = cb_PLoaioutput.Text.Trim();
                    KhachHang1 = cb_KhachHangoutput.Text.Trim();
                    GhiChu1 = txt_GhiChuoutput.Text.Trim();
                    TenCT1 = "";
                    if (cb_MaCToutput.Text != "")
                    {
                        TenCT1 = prs.substring_left(cb_MaCToutput.Text, child);
                    }
                    if(TenCT1=="")
                    {
                        TenCT1 = cb_MaCToutput.Text;
                    }

                    DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin có Số Serial " + txt_Serialoutput.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
                        string_temp=txt_Serialoutput.Text;
                        for (i = dataGridView4.Rows.Count - 2; i >= 0; i--)
                        {
                            if (string_temp == dataGridView4.Rows[i].Cells["Serial"].Value.ToString())
                            {
                                if (dataGridView4.Rows[i].Cells["SoKhoi"].Value.ToString() == "0")
                                {
                                    cmd.Parameters.AddWithValue("@Serial", txt_Serialoutput.Text);
                                    cmd.Parameters.AddWithValue("@SoBB", SoBB1);
                                    cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                                    cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                                    cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                                    cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                                    cmd.Parameters.AddWithValue("@ADorKD", AdorKd1);
                                    cmd.Parameters.AddWithValue("@SoXe", "");
                                    cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                    cmd.Parameters.AddWithValue("@ChieuDay", CDay_d);
                                    cmd.Parameters.AddWithValue("@ChieuRong", CRong_d);
                                    cmd.Parameters.AddWithValue("@ChieuDai", CDai_d);
                                    cmd.Parameters.AddWithValue("@SLNhap", "0");
                                    cmd.Parameters.AddWithValue("@SLXuat", SoLuong_d);
                                    cmd.Parameters.AddWithValue("@DVT", DVT1);
                                    cmd.Parameters.AddWithValue("@MaCT", TenCT1);
                                    cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                    cmd.Parameters.AddWithValue("@NguonGo", NguonGo1);
                                    cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                                    cmd.Parameters.AddWithValue("@UsersUpdate", lb_user.Text);
                                    cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                    cmd.ExecuteNonQuery();
                                    cmd.Parameters.Clear();
                                    MessageBox.Show("Chỉnh sửa thành công");
                                    bt_resertoutput_Click(null, null);
                                    
                                }
                                else
                                    MessageBox.Show("Dữ liệu thuộc nhóm Mặt Lã. Không sửa được, bạn có thể xóa, sau đó thêm mới");
                                break;
                            }
                        }
                           
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }


        }

        private void bt_editoutput_Click(object sender, EventArgs e)
        {
            edit_output();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txt_Serialoutput.Visible = true;
            txt_Serialoutput.Enabled = true;
        }

        private void bt_prevoutput_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView4.Rows.Count > 1)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_output(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_nextoutput_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView4.Rows.Count > 1)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView4.Rows.Count - 2)
                    {
                        i++;
                        Display_output(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_searchoutput_Click(object sender, EventArgs e)
        {
            search_out();
        }
        private void delete_output()
        {
            DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa Thông Tin có Số Serial " + txt_Serialoutput.Text, "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                try
                {
                    string datecheck = @"select datediff(dd,NgayInsert,getdate()) from dbo.KhoGo where Serial='" + txt_Serialoutput.Text + "'";
                    //string delete = @"delete from dbo.KhoTP_NK where Serial='" + lb_serialoutput.Text + "'";
                    Data dta = new Data();
                    SqlConnection Conn = dta.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;
                    cmd.CommandText = datecheck;
                    int date_count = int.Parse(cmd.ExecuteScalar().ToString());
                    if (date_count <= 1)
                    {
                        cmd.CommandText = "delete from dbo.KhoGo where Serial=" + txt_Serialoutput.Text;
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Dữ liệu được xóa thành công");

                        txt_Serialoutput.Text = "0";
                    }
                    else
                    {
                        MessageBox.Show("Quá thời gian cho phép,Không được phép xóa");
                    }
                    Conn.Close();
                    cmd.Dispose();
                    for (int i = dataGridView4.Rows.Count - 2; i >= 0; i--)
                    {
                        dataGridView4.Rows.RemoveAt(i);
                    }

                    bt_resertoutput_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void bt_deleteoutput_Click(object sender, EventArgs e)
        {
            delete_output();
        }

        private void print_output()
        {
            try
            {
                if (txt_Ngayoutput.Text != "")
                {
                    Process prs = new Process();
                    Ngay1 = txt_Ngayoutput.Text;
                    string[] temp = Ngay1.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    string_temp = "0";
                    string print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLXuat,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLXuat)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH, 
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLXuat>0 and dbo.KhoGo.Ngay='" + Ngay1 + "'";
                    if (txt_CDayoutput.Text != "")
                    {
                        print_NKTG += @" and dbo.KhoGo.ChieuDay=" + txt_CDayoutput.Text;
                    }
                    if (txt_CDayoutput.Text != "" && txt_CRongoutput.Text != "" && txt_CDaioutput.Text != "")
                    {
                        print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLXuat,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLXuat)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH, 
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLXuat>0 and dbo.KhoGo.ChieuDay=" + txt_CDayoutput.Text + " and dbo.KhoGo.ChieuRong=" + txt_CRongoutput.Text + " and dbo.KhoGo.ChieuDai=" + double.Parse(txt_CDaioutput.Text).ToString() + "  and dbo.KhoGo.Ngay='" + Ngay1 + "'";
                    }
                    if (txt_SoBBoutput.Text != "")
                    {
                        print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLXuat,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLXuat)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH,
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLXuat>0 and dbo.KhoGo.SoBB='" + txt_SoBBoutput.Text + "'";
                        string_temp = "1";
                    }
                    if (txt_SoCToutput.Text != "")
                    {
                        print_NKTG = @"SELECT     dbo.KhoGo.Serial as Serial_NK, dbo.KhoGo.ChungTu, dbo.KhoGo.PhanLoai as MaLoai, dbo.KhoGo.ChieuDay as ChieuDaySC, dbo.KhoGo.ChieuRong as ChieuRongSC, dbo.KhoGo.ChieuDai as ChieuDaiSC, dbo.KhoGo.SLXuat,(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai*dbo.KhoGo.SLXuat)/1000000000 as SoKhoi,
                       dbo.KhoGo.KhachHang, dbo.LyDo.LyDo as TenChiTiet, dbo.KhoGo.NoiGiao as MaKH, dbo.KhoGo.NoiNhan as TenKH,
                      dbo.KhoGo.SoBB, dbo.KhoGo.ADorKD,KhoGo.LoaiGo
                        FROM         dbo.KhoGo left join dbo.LyDo on dbo.LyDo.MaLyDo=dbo.KhoGo.LyDo where SLXuat>0 and dbo.KhoGo.ChungTu='" + txt_SoCToutput.Text + "'";
                        string_temp = "2";
                    }
                    DataSet ds = prs.dts(print_NKTG);
                    Fr_Report FrR = new Fr_Report();
                    if(string_temp=="0")
                        Ngay1 = "Ngày nhập: "+txt_Ngayoutput.Text;
                    if (string_temp == "1")
                        Ngay1 = "Số BB: " + txt_SoBBoutput.Text;
                    if (string_temp == "2")
                        Ngay1 = "Số CT: " +txt_SoCToutput.Text;
                    FrR.report_KhoGo_Xuat(ds, Ngay1);
                    FrR.Show();
                    ds.Clear();

                }
                else
                    MessageBox.Show("Chọn ngày cần in");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void bt_printoutput_Click(object sender, EventArgs e)
        {
            print_output();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridView1.CurrentCell.RowIndex;
                    //MessageBox.Show(r.ToString());
                    sum_temp = double.Parse(lb_Tonginput.Text) - (double.Parse(dataGridView1.Rows[r].Cells["SoLuonginput"].Value.ToString())*double.Parse(dataGridView1.Rows[r].Cells["CDayinput"].Value.ToString())*double.Parse(dataGridView1.Rows[r].Cells["CRonginput"].Value.ToString())*double.Parse(dataGridView1.Rows[r].Cells["CDaiinput"].Value.ToString()))/1000000000;
                    lb_Tonginput.Text = string.Format("{0:###.####}", sum_temp);
                    dataGridView1.Rows.RemoveAt(r);
                    //MessageBox.Show("aaaa");
                    int j = dataGridView1.Rows.Count - 1;
                    dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["CDayinput"];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (dataGridView1.Rows.Count == 1)
                lb_Tonginput.Text = "0";
        }

        private void dataGridView3_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView3.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridView3.CurrentCell.RowIndex;
                    //MessageBox.Show(r.ToString());
                    sum_temp = double.Parse(lb_Tongoutput.Text) - (double.Parse(dataGridView3.Rows[r].Cells["SoLuongoutput"].Value.ToString()) * double.Parse(dataGridView3.Rows[r].Cells["CDayoutput"].Value.ToString()) * double.Parse(dataGridView3.Rows[r].Cells["CRongoutput"].Value.ToString()) * double.Parse(dataGridView3.Rows[r].Cells["CDaioutput"].Value.ToString())) / 1000000000;
                    lb_Tongoutput.Text = string.Format("{0:###.####}", sum_temp);
                    dataGridView3.Rows.RemoveAt(r);
                    //MessageBox.Show("aaaa");
                    int j = dataGridView3.Rows.Count - 1;
                    dataGridView3.CurrentCell = dataGridView3.Rows[j].Cells["CDayoutput"];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (dataGridView3.Rows.Count == 1)
                lb_Tongoutput.Text = "0";
        }
        private void SearchTK()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                string_temp = @"select * from dbo.KhoGo_TK";
                if (txt_CDayTK.Text != "")
                {
                    if (txt_CRongTK.Text == "")
                        string_temp += " where ChieuDay=" + txt_CDayTK.Text;
                    if (txt_CRongTK.Text != "")
                        string_temp += " where ChieuDay=" + txt_CDayTK.Text + " and ChieuRong=" + txt_CRongTK.Text;
                }
                if (txt_CDayTK.Text == "")
                {
                    if (txt_CRongTK.Text != "")
                        string_temp += " where ChieuRong=" + txt_CRongTK.Text;
                }
                DataSet ds = prs.dts(string_temp);
                if (ds.Tables[0].Rows.Count == 0)
                    MessageBox.Show("Không có dữ liệu");
                else
                {
                    if (dataGridView5.Rows.Count > 1)
                        dataGridView5.Rows.Clear();
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        AdorKd1 = ds.Tables[0].Rows[i]["ADorKD"].ToString();
                        LoaiGo1 = ds.Tables[0].Rows[i]["LoaiGo"].ToString();
                        CDay1 = ds.Tables[0].Rows[i]["ChieuDay"].ToString();
                        CRong1 = ds.Tables[0].Rows[i]["ChieuRong"].ToString();
                        CDai1 = ds.Tables[0].Rows[i]["ChieuDai"].ToString();
                        SoLuong_d = double.Parse(ds.Tables[0].Rows[i]["SLTon"].ToString());
                        
                        SoLuong1 = string.Format("{0:###,###,###,###.#}", SoLuong_d);
                        sum_temp = double.Parse(ds.Tables[0].Rows[i]["SoKhoi"].ToString());
                        dataGridView5.Rows.Add(LoaiGo1, AdorKd1, CDay1, CRong1, CDai1, SoLuong1,sum_temp);
                    }
                    prs.formatcolordatagrid(dataGridView5);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_searchTK_Click(object sender, EventArgs e)
        {
            SearchTK();
        }
        private DataSet report_search()
        {
            Process prs = new Process();
            DataSet ds = new DataSet();
            SqlConnection Conn = prs.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            string[] temp = txt_NgayStart.Text.Split('/');
            Ngay_start1 = temp[1] + "/" + temp[0] + "/" + temp[2] + " 00:00:00";
            string[] temp2 = txt_Ngayend.Text.Split('/');
            Ngay_End1 = temp2[1] + "/" + temp2[0] + "/" + temp2[2] + " 23:59:59";
            cmd.CommandText = "prc_KhoGo_TH";
            cmd.Parameters.AddWithValue("@datebegin", Ngay_start1);
            cmd.Parameters.AddWithValue("@dateend", Ngay_End1);
            ds = prs.dts_sql(cmd);

                    /*Search = @"select qry2.MaCT,Load_CTSP_Mua.TenSP,Load_CTSP_Mua.TenChiTiet,Load_CTSP_Mua.SoLuongCT,qry2.SLNhap,qry2.SLNhap/SoLuongCT as DBNhapPS,qry2.SLXuat,qry2.SLXuat/SoLuongCT as DBXuatPS,isnull(qry2.TonDau,0) as TonDau,qry2.TonPS,qry2.TonPS/SoLuongCT as DBTonPS,(qry2.TonPS+isnull(qry2.TonDau,0) )*Load_CTSP_Mua.ChieuDaySC*Load_CTSP_Mua.ChieuRongSC*Load_CTSP_Mua.ChieuDaiSC/1000000000 as SoKhoi
                            from (select MaCT,sum(SLNhap) as SLNhap,sum(SLXuat) as SLXuat,sum(TonDau) as TonDau,sum(TonPS)as TonPS 
                            from((select MaCT,sum(SLNhap) as SLNhap,sum(SLXuat) as SLXuat,sum(SLNhap)-sum(SLXuat) as TonPS,TonDau=0  from dbo.KV1_KTG
                            where Ngay>='" + Ngay_start1 + " 00:00:00' and Ngay<='" + Ngay_End1 + " 23:59:59' Group by MaCT) union all (select MaCT,SLNhap=0,SLXuat=0,TonPS=0,sum(SLNhap)-sum(SLXuat) as TonDau from dbo.KV1_KTG where Ngay<'" + Ngay_start1 + " 00:00:00' group by MaCT)) as qry1 group by MaCT) as qry2 inner join dbo.Load_CTSP_Mua on qry2.MaCT=Load_CTSP_Mua.MaChiTiet where Load_CTSP_Mua.MaSP='" + MaSP1 + "'";*/
                    //Test



              
            ds = prs.dts_sql(cmd);
            cmd.Dispose();
            return ds;
        }

        private void bt_printreport_Click(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                DataSet ds = new DataSet();
                ds = Report_KhoGo();
                Fr_Report FrR = new Fr_Report();
                Ngay_start1 = txt_NgayStart.Text;
                Ngay_End1 = " - " + txt_Ngayend.Text;
                if (cb_NoiGiaoreport.Text != "")
                    NoiGiao1 = "Nơi giao: " + prs.substring_left(cb_NoiGiaoreport.Text, child);
                if (cb_NoiNhanreport.Text != "")
                    NoiNhan1 = "Nơi nhận: " + prs.substring_left(cb_NoiNhanreport.Text, child);
                if (cb_NoiGiaoreport.Text != "" || cb_NoiNhanreport.Text != "")
                    FrR.report_KhoGoNGN(ds, Ngay_start1, Ngay_End1, NoiGiao1, NoiNhan1);
                else
                    FrR.report_KhoGoTH(ds, Ngay_start1, Ngay_End1,NoiGiao1,NoiNhan1);
                FrR.Show();
                ds.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lb_user_TextChanged(object sender, EventArgs e)
        {
            Check_User();
        }

        private void cb_QuyCachoutput_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs = new Process();
            if (cb_QuyCachoutput.Text != "")
            {
                try
                {
                    string_temp = prs.substring_right(cb_QuyCachoutput.Text, child);
                    if (chk_MatLaoutput.Checked == true && chk_MatLaoutput.Visible == true)
                    {
                        lb_KhoiTK.Text = "0";
                        lb_TonKho.Text = "0";
                    }
                    else
                    {
                        if (string_temp != "")
                        {

                            string[] temp = string_temp.Split('x');
                            txt_CDayoutput.Text = temp[0].ToString().Trim();
                            txt_CRongoutput.Text = temp[1].ToString().Trim();
                            txt_CDaioutput.Text = temp[2].ToString().Trim();
                            Number_temp = "0";
                            GhiChu1 = "0";
                            SqlConnection Conn = prs.Connect();
                            Conn.Open();
                            SqlCommand cmd = new SqlCommand();
                            cmd.Connection = Conn;
                            string_temp = @"select SLTon,SoKhoi from dbo.KhoGo_TK where LoaiGo='" + cb_LoaiGooutput.Text.Trim() + "' and ADorKD='" + cb_AdorKDoutput.Text.Trim() + "' and ChieuDay=" + txt_CDayoutput.Text + " and ChieuRong=" + txt_CRongoutput.Text + " and ChieuDai=" + double.Parse(txt_CDaioutput.Text).ToString();
                            cmd.CommandText = string_temp;
                            SqlDataReader rd = cmd.ExecuteReader();
                            i = 0;
                            while (rd.Read())
                            {
                                //MessageBox.Show(string_temp);
                                Number_temp = rd[0].ToString();
                                GhiChu1 = rd[1].ToString();
                            }
                            lb_TonKho.Text = Number_temp;
                            lb_KhoiTK.Text = GhiChu1;
                            Conn.Close();
                            cmd.Dispose();
                            temp.Clone();
                            rd.Close();
                        }
                        else
                            MessageBox.Show("Nhập sai quy cách");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Kiểm tra lại thông tin nhập (AD/KD, loại gỗ..)",ex.Message);
                }
            }
        }

        private void cb_QuyCachinput_SelectedIndexChanged(object sender, EventArgs e)
        {
             
             if (cb_QuyCachinput.Text != "")
             {
                 try
                 {
                     Process prs = new Process();
                     string_temp = prs.substring_right(cb_QuyCachinput.Text, child);
                     if (string_temp != "")
                     {
                         string[] temp = string_temp.Split('x');
                         txt_CDayinput.Text = temp[0].ToString().Trim();
                         txt_CRonginput.Text = temp[1].ToString().Trim();
                         txt_CDaiinput.Text = temp[2].ToString().Trim();

                     }
                     else
                         MessageBox.Show("Nhập sai quy cách");
                 }

                 catch (Exception ex)
                 {
                     MessageBox.Show("Kiểm tra lại thông tin nhập (AD/KD, loại gỗ..)", ex.Message);
                 }
             }
        }

        private void chk_Enableinput_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Enableinput.Checked == true)
            {
                txt_CDaiinput.Enabled = true;
                txt_CDayinput.Enabled = true;
                txt_CRonginput.Enabled = true;
                chk_Enableinput.Text = "Enable";
            }
            if (chk_Enableinput.Checked == false)
            {
                txt_CDaiinput.Enabled = false;
                txt_CDayinput.Enabled = false;
                txt_CRonginput.Enabled = false;
                chk_Enableinput.Text = "Disable";
            }
        }

        private void chk_Enableoutput_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Enableoutput.Checked == true)
            {
                txt_CRongoutput.Enabled = true;
                txt_CDaioutput.Enabled = true;
                txt_CDayoutput.Enabled = true;
                chk_Enableoutput.Text = "Enable";
            }
            if (chk_Enableoutput.Checked == false)
            {
                txt_CRongoutput.Enabled =false;
                txt_CDaioutput.Enabled = false;
                txt_CDayoutput.Enabled = false;
                chk_Enableoutput.Text = "Disable";
            }
        }
        private DataSet Report_KhoGo()
        {
            Process prs = new Process();
            DataSet ds = new DataSet();
            CDay1 = txt_CDayreport.Text;
            CRong1 = txt_CRongreport.Text;
            CDai1 = txt_CDaireport.Text;
            NoiGiao1 = "";
            NoiNhan1 = "";
            if (cb_NoiGiaoreport.Text != "")
                {
                    NoiGiao1 = prs.substring_right(cb_NoiGiaoreport.Text, child);
                }
                if (cb_NoiNhanreport.Text != "")
                {
                    NoiNhan1 = prs.substring_right(cb_NoiNhanreport.Text, child);
                }
            string[] temp = txt_NgayStart.Text.Split('/');
            Ngay_start1 = temp[1] + "/" + temp[0] + "/" + temp[2] + " 00:00:00";
            string[] temp2 = txt_Ngayend.Text.Split('/');
            Ngay_End1 = temp2[1] + "/" + temp2[0] + "/" + temp2[2] + " 23:59:59";
            LoaiGo1 = cb_LoaiGoreport.Text;
            AdorKd1 = cb_AdorKDreport.Text;
            if (LoaiGo1 == "")
            {
                MessageBox.Show("Cho biết loại gỗ cần xem");
                
            }
            else
            {
                if (NoiGiao1 == "" && NoiNhan1 == "")
                {
                    string_temp = @"select qry1.LoaiGo,qry1.ADorKD, qry1.ChieuDay,  qry1.ChieuRong,  qry1.ChieuDai,sum(qry1.TonDau) as TonDau,sum(qry1.SoKhoiTD) as SoKhoiTD,sum(qry1.SLNhap) as SLNhap,sum(qry1.DBNhap) as DBNhap,sum(qry1.SLXuat) as SLXuat,sum(qry1.DBXuat) as DBXuat,sum(qry1.TonDau)+sum(qry1.SLNhap)-sum(qry1.SLXuat) as SLTon,sum(qry1.SoKhoiTD+qry1.SoKhoiPS) as SoKhoi
                                        from
                                        (SELECT     dbo.KhoGo.LoaiGo,dbo.KhoGo.ADorKD, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai, TonDau=0,SoKhoiTD=0,sum(dbo.KhoGo.SLNhap)as SLNhap,sum(SLNhap*ChieuDay*ChieuRong*ChieuDai/1000000000+SLNhap*SoKhoi) as DBNhap,sum(dbo.KhoGo.SLXuat) as SLXuat,sum(SLXuat*ChieuDay*ChieuRong*ChieuDai/1000000000+SLXuat*SoKhoi) as DBXuat,sum((SLNhap-SLXuat)*(ChieuDay*ChieuRong*ChieuDai/1000000000+SoKhoi)) as SoKhoiPS";
                   string_temp +=@"                     FROM         dbo.KhoGo  where dbo.KhoGo.Ngay>='"+Ngay_start1+"' and dbo.KhoGo.Ngay<='"+Ngay_End1+"'";
                   string_temp +=@"                     group by dbo.KhoGo.LoaiGo, dbo.KhoGo.ADorKD, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai
                                        union all
                                        SELECT     dbo.KhoGo.LoaiGo, dbo.KhoGo.ADorKD, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai, sum(dbo.KhoGo.SLNhap)-sum(dbo.KhoGo.SLXuat) as TonDau,SUM((SLNhap-SLXuat)*(ChieuDay*ChieuRong*ChieuDai/1000000000+SoKhoi)) as SoKhoiTD,SLNhap=0,DBNhap=0,SLXuat=0,DBXuat=0,SoKhoiPS=0
                                        FROM         dbo.KhoGo where dbo.KhoGo.Ngay<'"+Ngay_start1+"' group by dbo.KhoGo.LoaiGo, dbo.KhoGo.ADorKD, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai) as qry1 where LoaiGo='"+LoaiGo1+"'";

                }
                else
                {
                    string_temp = @"SELECT     dbo.KhoGo.LoaiGo,dbo.KhoGo.ADorKD,NoiGiao,NoiNhan,NguonGo as NguonGoc, dbo.KhoGo.ChieuDay, dbo.KhoGo.ChieuRong, dbo.KhoGo.ChieuDai,(dbo.KhoGo.SLNhap+dbo.KhoGo.SLXuat) as SoLuong,(dbo.KhoGo.SLNhap+dbo.KhoGo.SLXuat)*(dbo.KhoGo.ChieuDay*dbo.KhoGo.ChieuRong*dbo.KhoGo.ChieuDai/1000000000+SoKhoi) as SoKhoi,ChungTu,CONVERT(varchar(10),Ngay,103) as Ngay,SoBB
                                    FROM         dbo.KhoGo where LoaiGo='"+LoaiGo1+"'"; 
                }
                    if (CDay1 != "")
                        string_temp += "and ChieuDay=" + CDay1;
                    if (CRong1 != "")
                        string_temp += "and ChieuRong=" + CRong1;
                    if (CDai1 != "")
                        string_temp += " and ChieuDai=" + CDai1;
                    if (NoiGiao1 != "")
                        string_temp += " and NoiGiao='" + NoiGiao1 + "'";
                    if (NoiNhan1 != "")
                        string_temp += " and NoiNhan='" + NoiNhan1 + "'";
                    if (AdorKd1 != "")
                        string_temp += " and ADorKD='" + AdorKd1 + "'";
                    if (NoiGiao1 == "" && NoiNhan1 == "")
                        string_temp += " group by qry1.LoaiGo,qry1.ADorKD, qry1.ChieuDay,  qry1.ChieuRong,  qry1.ChieuDai,qry1.ChieuDay";
                   
                
               
                ds = prs.dts(string_temp);
            }
            return ds;

        }

        private void Fr_KhoGo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                if (e.KeyCode.Equals(Keys.N))
                {
                    if (tab_control.SelectedTab == tab_Nhap)
                    {

                        MessageBox.Show("Bạn đã mở chức năng thêm hàng hóa trong chứng từ có sẵn, vui lòng nhập số chứng từ");
                        txt_SoCTinput.Enabled = true;
                        txt_SoCTinput.Focus();
                    }
                    if (tab_control.SelectedTab == tab_Xuat)
                    {
                        MessageBox.Show("Bạn đã mở chức năng thêm hàng hóa trong chứng từ có sẵn, vui lòng nhập số chứng từ");
                        txt_SoCToutput.Enabled = true;
                        txt_SoCToutput.Focus();
                    }
                }
                if (e.KeyCode.Equals(Keys.M))
                {
                    if (tab_control.SelectedTab == tab_Nhap)
                        chk_MatLa.Visible = true;
                    if (tab_control.SelectedTab == tab_Xuat)
                        chk_MatLaoutput.Visible = true;
                }
            }
        }

        private void bt_excelreport_Click(object sender, EventArgs e)
        {
            string[] temp = txt_NgayStart.Text.Split('/');
            Ngay_start1 = temp[1] + "/" + temp[0] + "/" + temp[2] + " 00:00:00";
            string[] temp2 = txt_Ngayend.Text.Split('/');
            Ngay_End1 = temp2[1] + "/" + temp2[0] + "/" + temp2[2] + " 23:59:59";
            string temp_string = @"SELECT  [Serial]
                                  ,[ChungTu]
                                  ,[SoBB]
                                  ,[LoaiGo]
                                  ,[NoiGiao]
                                  ,[NoiNhan]
                                  ,[LyDo]
                                  ,[ADorKD]
                                  ,[SoXe]
                                  ,[PhanLoai]
                                  ,[ChieuDay]
                                  ,[ChieuRong]
                                  ,[ChieuDai]
                                  ,[SoKhoi]
                                  ,[SLNhap]
                                  ,[SLXuat]
                                  ,[DVT]
                                  ,[MaCT]
                                  ,[KhachHang]
                                  ,[NguonGo]
                                  ,[GhiChu]
                                  ,[Ngay]
                                  ,[NgayInsert]
                                  FROM [dbo].[KhoGo] where Ngay>='" + Ngay_start1 + "' and Ngay<='" + Ngay_End1 + "'";
            Process prs = new Process();
            DataSet ds = new DataSet();
            try
            {
                ds = prs.dts(temp_string);
                prs.ExportExcel(ds.Tables[0], "C:\\Huynh\\KhoGo_Data.xlsx", 6, 1);
                ds.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tab_BangKe_Click(object sender, EventArgs e)
        {
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            Conn.Open();
            /*SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;

            cmd.CommandText = @"select CONVERT(nvarchar(10),getdate(),101)";*/
            try
            {
                string[] temp = txt_NgayBK.Text.Split('/');
                string_temp = temp[1] + "/" + temp[0] + "/" + temp[2];
                //string_temp = cmd.ExecuteScalar().ToString();
                string_temp = @"SELECT [Serial],[ChungTu],[SoBB],[LoaiGo],[NoiGiao],[NoiNhan],[LyDo],[ADorKD],[PhanLoai]
                    ,[ChieuDay],[ChieuRong],[ChieuDai],[SLNhap],[SLXuat],[DVT],[NguonGo],[GhiChu],convert(varchar(10),[Ngay],103) as Ngay,[NgayInsert]
                    FROM [dbo].[KhoGo]
                 
                  where NgayInsert>='" + string_temp + "' order by Serial";
                DataSet ds = dta.dts(string_temp);
                dataGridViewX1.DataSource = ds.Tables[0];
                for (i = dataGridViewX1.Rows.Count - 2; i >= 0; i--)
                {
                    if (dataGridViewX1.Rows[i].Cells["SLNhap_BK"].Value.ToString()!="0")
                        dataGridViewX1.Rows[i].DefaultCellStyle.BackColor = Color.AliceBlue;
                    if (dataGridViewX1.Rows[i].Cells["SLNhap_BK"].Value.ToString() == "0")
                        dataGridViewX1.Rows[i].DefaultCellStyle.BackColor = Color.Silver;
                }
                temp.Clone();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                Conn.Close();
                //cmd.Dispose();
            }
        }

        private void chk_MatLa_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_MatLa.Checked == true)
            {
                lb_m3.Visible = true;
                txt_CRonginput.Text = "0";
                txt_CRonginput.Visible = false;
                
            }
            if (chk_MatLa.Checked == false)
            {
                lb_m3.Visible = false;
                txt_CRonginput.Text = "";
                txt_CRonginput.Visible = true;
            }


        }

        private void chk_MatLaoutput_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_MatLaoutput.Checked == true)
            {
                lb_M3output.Visible = true;
                txt_CRongoutput.Text = "0";
                txt_CRongoutput.Visible = false;

            }
            if (chk_MatLaoutput.Checked == false)
            {
                lb_M3output.Visible = false;
                txt_CRongoutput.Text = "";
                txt_CRongoutput.Visible = true;
            }
        }
        private void search_NT()
        {

        }

        private void tabItem4_Click(object sender, EventArgs e)
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            Conn.Open();
            try
            {
                string_temp = @"select ChungTu from dbo.KhoGo_ChungTu where CTNghiemThu is null";
                SqlDataReader rd = cmd.ExecuteReader();
                if (cb_ChungTuQL.Items.Count > 0)
                    cb_ChungTuQL.Items.Clear();
                while (rd.Read())
                {
                    cb_ChungTuQL.Items.Add(rd[0].ToString());
                }
                rd.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_searchQL_Click(object sender, EventArgs e)
        {

        }

        
    }
}
